package com.zhonghui.modlules.user.eshopService.Impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.eshop.apiCode.EshopException;
import com.eshop.service.Impl.BaseServiceImpl;
import com.zhonghui.common.eshopDomain.ShopUser;
import com.zhonghui.common.eshopEnum.OrderInfoEnum;
import com.zhonghui.common.eshopEnum.PayTypeEnum;
import com.zhonghui.modlules.user.eshopDomain.UserRecharge;
import com.zhonghui.modlules.user.eshopMapper.UserRechargeMapper;
import com.zhonghui.modlules.user.eshopService.UserRechargeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@Service
@Transactional(
        propagation = Propagation.SUPPORTS,
        readOnly = true,
        rollbackFor = {Exception.class}
)
public class UserRechargeServiceImpl extends BaseServiceImpl<UserRechargeMapper, UserRecharge> implements UserRechargeService {
    @Autowired
    private UserRechargeMapper yxUserRechargeMapper;
    @Override
    public String addRecharge(ShopUser user, String price, String paidPrice) {
        if(!StrUtil.isBlank( price ) && !StrUtil.isBlank( paidPrice )){
            UserRecharge userRecharge = new UserRecharge();
            String orderSn = IdUtil.getSnowflake( 0L, 0L ).nextIdStr();
            userRecharge.setNickname( user.getNickname() );
            userRecharge.setOrderId( orderSn );
            userRecharge.setUid( user.getUid() );
            userRecharge.setPrice( new BigDecimal( price ) );
            userRecharge.setGivePrice( new BigDecimal( paidPrice ) );
            userRecharge.setRechargeType( PayTypeEnum.WEIXIN.getValue() );
            userRecharge.setPaid( OrderInfoEnum.PAY_CHANNEL_0.getValue() );
            yxUserRechargeMapper.insert( userRecharge );
            return orderSn;
        }else {
            throw new EshopException( "参数非法" );
        }
    }
}
